#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define HASH_SIZE 101
typedef struct Nlist{
	struct Nlist* next;
	char* name;
	char* text;
}nlist;

static nlist* hashtab[HASH_SIZE];

unsigned hash(char* name){
	unsigned hashval;
	for(hashval=0;*name!='\0';name++){
		hashval=*name + 31*hashval;
	}
	return hashval % HASH_SIZE;
}

nlist* lookup(char* name){
	unsigned hashval=hash(name);
	nlist* np=hashtab[hashval];
	for(;np!=NULL;np=np->next){
		if(strcmp(np->name,name) ==0 )
			return np;
	}
	return NULL;
}

nlist* install(char* name,char* text){
	nlist* np;
	if( (np=lookup(name))==NULL){
		np=(nlist*)malloc(sizeof(*np));
		if(np==NULL || (np->name=strdup(name))==NULL)
			return NULL;
		unsigned hashval=hash(name);
		np->next=hashtab[hashval];
		hashtab[hashval]=np;
	}else{
		if( (np->name=strdup(name))==NULL)
			return NULL;
	}
	if( (np->text=strdup(text)) ==NULL)
		return NULL;
	return np;
}

int main(){
	install("a","apple");
	install("b","beep");
	install("c","cid");
	install("d","apple");
	nlist* fp=lookup("d");
	if(fp!=NULL)
		printf("%s,%s\n",fp->name,fp->text);
}
